package com.team.frame.interceptor;

import com.team.frame.interceptor.annotation.TagAction;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by lidan on 2017/2/6.
 */
public class HttpInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler.getClass().isAssignableFrom(HandlerMethod.class)) {
            TagAction tagAction = ((HandlerMethod) handler).getMethodAnnotation(TagAction.class);
            if (tagAction == null || tagAction.mustLogin() == false)
                return true;
            else {
                if (false)
                    //可以进行Session验证
                    return true;
                else {
                    //跳转统一登录
                    response.sendRedirect(request.getContextPath() + "/sample/login/index.html");
                    return false;
                }
            }
        } else
            return true;
    }
}

